package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import java.util.Set;
@Service(version = "1.0.0",interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);

        if(user==null){
            return null;
        }

        Set<Role> roles=roleDao.findRoleById(user.getId());
        if(roles!=null&&roles.size()>0){
            for (Role role : roles) {
                Set<Permission> permissions= permissionDao.findById(role.getId());
                if(permissions!=null&&permissions.size()>0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }

        return user;
    }

    @Override
    public Result findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> page= userDao.findCondition(queryPageBean.getQueryString());
        PageResult pageResult=new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getResult());
        System.out.println(pageResult);
        return Result.success(pageResult);
    }

    @Override
    public Result save(User user) {
        userDao.save(user);
        List<Integer> roleIds = user.getRoleIds();
        for (Integer roleId : roleIds) {
            userDao.addUserAndRole(user.getId(), roleId);
        }
        return Result.success(null);
    }

    @Override
    public Result update(User user) {
        userDao.update(user);
        userDao.deleteUserAndRole(user.getId());
        List<Integer> roleIds = user.getRoleIds();
        for (Integer roleId : roleIds) {
            userDao.addUserAndRole(user.getId(), roleId);
        }
        return Result.success(null);
    }

    @Override
    public Result delete(Integer id) {
        userDao.delete(id);
        return Result.success(null);
    }

    @Override
    public Result findAll() {
        List<Role>roleList =userDao.findAll();
        return Result.success(roleList);
    }

    @Override
    public Result findById(Integer id) {
        User user = userDao.findById(id);
        List<Role> roleList = userDao.findAll();
        List<Integer> role = userDao.findUserAndRole(id);
        final HashMap<String, Object> map = new HashMap<>();
        map.put("formData", user);
        map.put("tableData", roleList);
        map.put("roleIds", role);
        return Result.success(map);
    }

}
